package com.efounder.addressselector;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.efounder.mobilemall.R;
import com.utilcode.util.ConvertUtils;

/**
 * Created by will on 18-1-16.
 */

public class RightDialog extends Dialog {
    private AddressSelector selector;
    public static RightDialog dialog;

    public RightDialog(Context context) {
        super(context, R.style.right_dialog);
        init(context);
    }

    public RightDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    public RightDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    private void init(Context context) {
        selector = new AddressSelector(context);
        setContentView(selector.getView());
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        params.width = ConvertUtils.dp2px(300);
        window.setAttributes(params);
        selector.getBackView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dialog!=null)
                dialog.dismiss();
            }
        });

        window.setGravity(Gravity.RIGHT);
    }

    public void setOnAddressSelectedListener(OnAddressSelectedListener listener) {
        this.selector.setOnAddressSelectedListener(listener);
    }

    public static RightDialog show(Context context) {
        return show(context, null);
    }

    public static RightDialog show(Context context, OnAddressSelectedListener listener) {
        dialog = new RightDialog(context, R.style.right_dialog);
        dialog.selector.setOnAddressSelectedListener(listener);
        dialog.show();

        return dialog;
    }
}
